package com.livery.app.controller;

import java.awt.BorderLayout;
import java.awt.Window;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.annotations.Cascade;
import org.hibernate.criterion.Restrictions;

import com.livery.app.main.HibernateUtil;
import com.livery.app.model.AbstractWorkspaceModel;
import com.livery.app.view.WorkerView;
import com.livery.data.Booking;
import com.livery.data.Car;
import com.livery.data.Client;
import com.livery.data.Transaction;
import com.livery.data.Worker;
import com.livery.data.creators.ClientCreator;
import com.livery.data.creators.Creator;
import com.livery.data.creators.TransactionCreator;
import com.livery.app.model.search.CarSearch;
import com.livery.app.model.search.ClientSearch;

public class WorkspaceController implements AbstractWorkspaceController{

	private AbstractWorkspaceModel model;
	private WorkerView view;
	
	private Creator clientCreator;
	private Creator transactionCreator;
	
	public WorkspaceController(AbstractWorkspaceModel model){
		this.model = model;
		view = new WorkerView(model, this);
		view.createView();
	}
	
	@Override
	public void addCar() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addClient() {
		// TODO Auto-generated method stub
		if(clientCreator == null)
			clientCreator = new ClientCreator();
		//(clientCreator).setEmptyField();
		showCreator(clientCreator);
		downloadClientList(0, 0);
	}

	@Override
	public void addTransaction(Client client, Car car) {
		if(transactionCreator == null)
			transactionCreator = new TransactionCreator(client,car);
		//(clientCreator).setEmptyField();
		((TransactionCreator)transactionCreator).setClientCar(client,car);
		showCreator(transactionCreator);
		
	}

	@Override
	public void addWorker() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void archiveCar(Car car) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void archiveClient(Client client) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void archiveTransaction(Transaction transaction) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void archiveWorker(Worker worker) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void finalizeTransaction() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeBooking(Booking booking) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showCreator(Creator<?> creator) {
		creator.showCreator();
		
	}

	@Override
	public void updateCar(Car car) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateClient(Client client) {
		if(clientCreator == null)
			clientCreator = new ClientCreator();
		((ClientCreator) clientCreator).setObject(client);
		showCreator(clientCreator);
		downloadClientList(0, 0);
	}

	@Override
	public void updateTransaction(Transaction transaction) {
		if(transactionCreator == null)
			transactionCreator = new TransactionCreator();
		((TransactionCreator)transactionCreator).setObject(transaction);
		showCreator(transactionCreator);
		
	}

	@Override
	public void updateWorker(Worker worker) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean downloadCarList(int limit, int offset) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("from Car");
		model.setCarList(query.list());
		session.close();
		
		return true;
	}

	@Override
	public boolean downloadWorkerList(int limit, int offset) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("from Worker");
		model.setWorkerList(query.list());
		session.close();
		
		return true;
	}

	@Override
	public boolean downloadClientList(int limit, int offset) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("from Client");
		List<Client> list = query.list();
		model.setClientList(list);
		session.close();
		
		return true;
	}

	@Override
	public boolean downloadTransactionList(int limit, int offset) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("from Transaction tr join fetch tr.car join fetch tr.client join fetch tr.pickWorker");
		model.setTransactionList(query.list());
		session.close();
		
		return true;
	}

	@Override
	public boolean downloadBookingList(int limit, int offset) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("from Booking");
		model.setBookingList(query.list());
		session.close();
		
		return true;
	}

	@Override
	public void searchClient(String name, String surname, String documentID) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Client.class);
		ClientSearch client = new ClientSearch();
		client.setFirstName(name);
		client.setLastName(surname);
		client.setDocumentID(documentID);
		
		
		if(!client.getFirstName().equals("")) {
		    criteria.add(Restrictions.eq("firstName", client.getFirstName()));
		}
		if(!client.getLastName().equals("")){
			criteria.add(Restrictions.eq("lastName", client.getLastName()));
		}
		if(!client.getDocumentID().equals("")){
			criteria.add(Restrictions.eq("documentID", client.getDocumentID()));
		}
		model.setClientList(criteria.list());
		session.close();
	}

	@Override
	public void searchCar(String make, String model, Date productionYear) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Car.class);
		CarSearch car = new CarSearch();
		car.setMake(make);
		car.setModel(model);
		car.setProductionYear(productionYear);
		
		if(!car.getMake().equals("")){
		    criteria.add(Restrictions.eq("make", car.getMake()));
		}
		if(!car.getModel().equals("")){
			 criteria.add(Restrictions.eq("model", car.getModel()));
		}
		if(car.getProductionYear() != null){
			 criteria.add(Restrictions.eq("productionYear", car.getProductionYear()));
		}
		this.model.setCarList(criteria.list());
		session.close();
	}

}
